import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

@Component({
	selector: 'sa-calibration',
	templateUrl: './calibration.component.html',
})
export class CalibrationComponent implements OnInit {
	tableData = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	startTime_table:string = '';
	endTime_table:string = '';
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	carNumber:string = '';
	deviceId:string = '';
	jumpData:any; // 跳转页面接收到的数据
    initDateRange: any = {
        startTime:'',
        endTime:'',
	}// 时间组件需要渲染的值
	dateType: any =''; //默认来源
	carDeatailUseList = carTypeListObj.carDeatailUseList
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
			if(common.getCookie('jumpTo/langyun/calibration')){
                this.jumpData = JSON.parse(common.getCookie('jumpTo/langyun/calibration'));
                this.initDateRange.startTime = this.jumpData.startTime;
				this.initDateRange.endTime = this.jumpData.endTime;
				this.dateType = '1';
			}
			if (common.getCookie('calibrationDetail')) {
				this.jumpData = JSON.parse(common.getCookie('calibrationDetail'));
				this.initDateRange.startTime = this.jumpData.createTime;
				this.initDateRange.endTime = this.jumpData.createTime;
				this.dateType = '2';
            }
		});
	}

	ngOnInit() {
	    let that = this;
	    if(common.getCookie('jumpTo/langyun/calibration') || common.getCookie('calibrationDetail')) {
            setTimeout(()=>{
                that.load();
            },100)
			common.clearCookie('jumpTo/langyun/calibration');
			common.clearCookie('calibrationDetail');
        }else{
            this.load();
        }
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime_table = time.startTime;
		this.endTime_table = time.endTime;
	} 
	// 公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
        this.deviceId = data.deviceId;
        this.carNumber = data.carNumber;
    }
	//表格搜索
	table_search() {
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
    }
	//初始化加载list数据
	load() {
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId: this.selectedCompanyId,
//				deviceId: this.deviceId,
				carNumber: this.carNumber,
				startTime:this.startTime_table,
				endTime:this.endTime_table,
				type: '1',
				dateType: this.dateType
//				searchKey:this.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("calibrationData/list.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
			for(let item of this.tableData) {
				item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
			}
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//导出报表
	exportData() {
		let parameters = {
			companyId: this.selectedCompanyId,
//				deviceId: this.deviceId,
			carNumber: this.carNumber,
			startTime: this.startTime_table,
			endTime: this.endTime_table,
			type: '1',
			dateType: this.dateType
//				searchKey: this.searchKey,
		};
		let timeCheck = common.correctTime(parameters, ['type', 'dateType'])
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let paramsName = 'param';
		let _url = config.BaseURL + 'calibrationData/export.html';
		common.exprotDataFile(parameters,paramsName,_url);
	}
	// 删除
	delRow(row){
	    this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
            let reqdata = 'id=' + row.id;
            this.jsonApiService.http("calibrationData/deletedById.html", reqdata).subscribe(data => {
                this.jsonApiService.message(data.message);
                this.load();
            })
        })
	}
	// 页面跳转
	jumpCarList(row){
        let BaseURL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
        common.setCookie('jumpTo/system/car', JSON.stringify(row));
        window.open(BaseURL + '#/system/car');
	}

	getUseTypeNameL2(key) {
        if(key) {
            return this.carDeatailUseList[key];
        } else {
            return '';
        }
    }
}